﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Windows.Forms;
using System.Configuration;
using ECS.DAL;
using ECS.DAL.Entity;
using ECS.DAL.Component;
using ECS.Framework;
using ECS.BLL.Entity;
using ECS.BLL.Component;

using AutoUpdaterDotNET;

namespace E_ICS.Main
{
    public partial class frmLogin : Form
    {
        #region private properties
       
        BackgroundWorker _worker = null;
        #endregion

        #region Form event
        
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (CanValidateInput())
                {
                    Login();
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }
        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                lblVersion.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                _worker = new BackgroundWorker();
                _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                _worker.WorkerSupportsCancellation = true;
                _worker.RunWorkerAsync();
            
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    CommonMessageBox.ShowMessageError(e.Error);
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            CheckVersion();
        }

        private void password_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    Login();
                }
            }
            catch (Exception ex)
            {
               CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void txtUserId_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    password.Focus();
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void frmLogin_Shown(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }
        #endregion

        #region private methods
        private void CheckVersion()
        {
            string version = string.Empty;
            SearchInfo search = new SearchInfo();
            version = search.GetApplicationVersion();
            if (version != GlobalVariable.version)
            {
                AutoUpdater.Start(ConfigurationManager.AppSettings["Autoupdate"]);
            }
          
        }
        private bool CanValidateInput()
        {
            bool isResult = true;
            if (string.IsNullOrEmpty(txtUserId.Text))
            {
                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourceString(CommonResourceString.ConstantString.INVALID_USER_ID));
                txtUserId.Focus();
                isResult = false;
            }
            return isResult;
        }
        /// <summary>
        /// check user/password permission with appID and device name
        /// </summary>
        private void Login()
        {
            string appId = string.Empty;
            string device = GlobalVariable.Device;
            string clearUserFlag = string.Empty;
            string appVersion = GlobalVariable.version;
            
            try
            {
                appId = ConfigurationManager.AppSettings["appID"];
                clearUserFlag = ConfigurationManager.AppSettings["userflag"];
                UserAuthenticateComponent authen = new UserAuthenticateComponent();
                UserAuthenticate user = authen.GetUserAuthentication(txtUserId.Text, password.Text, appId, device, clearUserFlag, appVersion);
                if (user != null)
                {
                    if (user.Status.Equals("PASS"))
                    {
                        SearchInfo searchInfo = new SearchInfo();
                        GlobalVariable.userName = txtUserId.Text;
                        GlobalVariable.user = user;
                        GlobalVariable.user.UserName = txtUserId.Text;
                        GlobalVariable.user.ApplicationID = appId;
                        GlobalVariable.user.Device = device;
                        GlobalVariable.user.ClearUserFlag = clearUserFlag;
                        GlobalVariable.user.Version = appVersion;
                        GlobalVariable.scaleID = searchInfo.GetScaleId(GlobalVariable.user.Device);
                        GlobalVariable.fbl_fltNO = searchInfo.getFBLFlihtNo(GlobalVariable.user.Device);
                        //getScaleID(GlobalVariable.user.Device);
                       // getFBLfltNO(GlobalVariable.user.Device);
                        this.Visible = false;
                        this.Enabled = false;
                        Main.frmMain2 t = new Main.frmMain2();
                        t.Show();
                    }
                    else
                    {
                        CommonMessageBox.ShowMessageInfo(user.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (_worker.IsBusy)
                {
                    _worker.CancelAsync();
                }
                _worker.Dispose();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion
    }
}
